#include "grounpsessiondetailwidget.h"

GrounpSessionDetailWidget::GrounpSessionDetailWidget(QWidget* parent)
    :QDialog(parent)
{
    // 1.设置窗口属性
    this->setFixedSize(QSize(400, 600));
    this->setWindowTitle("群聊详情");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setStyleSheet("QWidget { background-color:rgb(160, 160, 160); }");

    // 2.垂直布局管理器
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(10);
    vlayout->setContentsMargins(20, 20, 20, 20);
    this->setLayout(vlayout);

    // 2.1滚动区域
    QScrollArea* scrollArea = new QScrollArea();
    scrollArea->setWidgetResizable(true);
    scrollArea->verticalScrollBar()->setStyleSheet("QScrollBar::vertical { width:2px; background-color:rgb(255, 255, 255); }");
    scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar::horizontal { height:0px; }");
    scrollArea->setStyleSheet("QScrollArea { border:none; }");

    QWidget* container = new QWidget();
    scrollArea->setWidget(container);

    // container 中的网格布局
    glayout = new QGridLayout();
    glayout->setSpacing(10);
    glayout->setContentsMargins(0, 0, 0, 0);
    glayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    container->setLayout(glayout);

    AvatarItem* avatarItem = new AvatarItem(QIcon(":/resource/image/add_friend.png"), "添加");
    glayout->addWidget(avatarItem, 0, 0, 1, 1);

    // 2.2群聊名称
    QLabel* groupNameTag = new QLabel("群聊名称");
    groupNameTag->setStyleSheet("QLabel { font-size:15px; font-weight:600; }");

    QLabel* groupNameLabel = new QLabel("海贼王-海军总部");
    groupNameLabel->setStyleSheet("QLabel { font-size:20px; }");

    QPushButton* modifyBtn = new QPushButton();
    modifyBtn->setFixedSize(QSize(30, 30));
    modifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    modifyBtn->setIconSize(QSize(30, 30));
    modifyBtn->setStyleSheet("QPushButton:pressed { background-color:rgb(230, 230, 230); }"
                             "QPushButton { border:none; }");

    QHBoxLayout* hlayout = new QHBoxLayout();
    hlayout->setSpacing(0);
    hlayout->setContentsMargins(0, 0, 0, 0);

    hlayout->addWidget(groupNameLabel);
    hlayout->addWidget(modifyBtn);

    // 2.3退出群聊按钮
    QPushButton* exitBtn = new QPushButton("退出群聊");
    exitBtn->setFixedSize(QSize(200, 50));
    exitBtn->setStyleSheet("QPushButton:pressed { background-color:rgb(230, 230, 230); }"
                           "QPushButton { font-size:20px; }");

    // 添加到垂直布局管理器中
    vlayout->addWidget(scrollArea);
    vlayout->addWidget(groupNameTag);
    vlayout->addLayout(hlayout);
    vlayout->addWidget(exitBtn, 0, Qt::AlignCenter);

    // 添加测试数据
#if TEST_F
    for(int i = 0;i < 30;i ++) {
        AvatarItem* item = new AvatarItem(QIcon(":/resource/image/default_avatar.png"), "路飞-" + QString::number(i));
        this->addMember(item);
    }
#endif
}

void GrounpSessionDetailWidget::addMember(AvatarItem *item)
{
    int MAX_COL = 4;
    if(col > MAX_COL) {
        row ++;
        col = 0;
    }
    glayout->addWidget(item, row, col, 1, 1);
    col ++;
}
